<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Homecontroller;
use Illuminate\Http\Request;
use Hash;
use Mail;
use DB;
use App\Http\Requests;
use App\Http\Requests\AdminLoginPostRequest;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;

class LoginController extends Controller
{
    /*
        显示后台登录页面
    */ 
        public function getLogin()
        {
            //显示模板
            return  view('admin.login.login');

        }

    /*
        后台登录验证
    */
        public function postCheck(AdminLoginPostRequest $request)
        {
            // dd($request->all());
            //检测用户是否存在
            $user = DB::table('dota_user')->where('email',$request->input('email'))->first();
            if ($user){

                        //用户存在,检测密码是否正确
                        if(Hash::check($request->input('password'), $user->password)){

                            //密码正确,检测权限
                           if (($user->qx) == 2){

                                    //有权限,登录成功
                                    session(['id'=>$user->id]);

                                    //跳转
                                    return redirect('/admin');

                            }else{
                                    //没有权限,登录失败
                                    return back()->with('error','您没有登录权限');
                            }

                        }else{
                            //密码不正确,登录失败
                             return back()->with('error','用户名或密码错误');

                        }         
                  
            }else{

                //用户不存在,登录失败
                return back()->with('error','用户名或密码错误');
          
            }
        }


     /*
         后台退出登录
    */
        public function getLogout()
        {
             //echo "222222";die;
            session()->forget('id');
            return redirect('/admin/login/login');
        }

    /*
        前台用户注册显示
    */
        public function register()
        {
            return view('home.register.register');
        }

    /*
        验证码
    */
        public function vcode()
        {
            ob_clean();//清除输出缓存区的内容
            //生成验证码图片的Builder对象，配置相应属性
            $builder = new CaptchaBuilder;
            //可以设置图片宽高及字体
            $builder->build($width = 160, $height = 36, $font = null);
            //获取验证码的内容
            $phrase = $builder->getPhrase();

            //把内容存入session
            session(['Vcode'=>$phrase]);

            //生成图片
            header("Cache-Control: no-cache, must-revalidate");
            header('Content-Type: image/jpeg');
            $builder->output();
            
        }

    /*
        前台用户注册
    */
        public function doRegister(Request $request)
        {
            // return back()->with('error','fhsjdhjkdsfhfjkhds');
            // dd($data);
            $revcode =  session()->get('Vcode');
            $this->validate($request, [
                
                'password' => 'required',
                'repeat' => 'required|same:password',
                'email' => 'required|email',
                'rand' => 'required',
                // |same:'.$revcode,
                'truename' => 'required',
                // |regex:/^[\u4E00-\u9FA5]{2,5}$/i',
                'id_card' => 'required',
                // |regex:/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/',
            ],[
                 
                 'password.required'=>'密码不能为空', 
                 'repassword.required'=>'确认密码不能为空',
                 'repassword.same'=>'两次密码不一致',
                 'email.required'=>'邮箱不能为空',
                 'email.email'=>'邮箱格式不正确',
                 'rand.required'=>'验证码不能为空',
                 // 'rand.regex'=>'验证码不正确',
                 // 'truename.regex'=>'真实姓名不正确',
                 'truename.required'=>'真实姓名不能为空',
                 // 'id_card.regex'=>'身份证号不正确',
                 'id_card.required'=>'身份证号不能为空',

        ]);


            $data = $request->only(['email','password','truename','id_card','_token']); 

            
            //密码加密
            $data['password'] = Hash::make($data['password']);
            $id = DB::table('dota_user')->insertGetId($data);

            if ($id) {

                $this->sendEmail($data['email'],$data['_token'] );

                  
                 return view('/home/email/success') ;                 
                
            }else{
                
                return back()->with('error','添加失败');
                    
            }
                

        }

    /*
        前台用户注册验证用户存在
    */
        public function checkuser(Request $request)
        {
           $email = $request->email;
            $res = DB::table('dota_user')->where('email',$email)->first();

            if ($res) {
                $num = 1;
            }else{
                $num = 0;
            }
            echo($num);
        }

    /*
        前台注册验证码验证
    */
        public function checkVcode(Request $request)
        {
            $vcode = $request->input('vcode');
            // var_dump($request);
            // echo "1";
            // $vcode = $request->vcode;
            // var_dump($vcode);
            // $revcode = session('Vcode');
           $revcode =  session()->get('Vcode');
            if ($vcode == $revcode){
                echo 1;
            }else{
                echo 0;
            }

             // echo $num;
        }

    /*
        发送激活邮件
    */
        public function sendEmail($email,$token)
        {
            // $id = $request->input('id');
            // $email = $request->input('email');
            // $_token = $request->input('_token');
            // dd($email,$id,$_token);
            // Mail::raw('狗蛋网欢迎你注册请点击.即可获取大量资源...', function ($message) {
            // //设置邮件标题
            // $message->subject('Dota2官方激活邮件');
            // //发送给谁
            // $message->to('misshanxi4@163.com');
            // });
         
            // Mail::send('home.email.email', ['email'=>$email,'id'=>$id], function ($message)use($email) {
            // $message->to($email)->subject('激活邮件');
            // return view('home.email.email',['email'=> $email]);
            Mail::send('home.email.email', ['email'=>$email,'token'=>$token], function ($message)use($email) {
                        $message->to($email)->subject('激活邮件');



            
            });
            

            // return view('home.email.email',['email'=> $email,'id'=> $id,'token'=>$token]);
        
        }

    /*
        进行用户激活
    */
            public function jihuo(Request $request)
            {
                // dd($request);
                $email = $request->email;
                $token = $request->token;
                $user = DB::table('dota_user')->where('email',$request->email)->first();
                $_token = $user->_token;

                if ($token == $_token) {

                   $res = DB::table('dota_user')->where('email',$request->email)->update(['status' => 1]);

                   return view('/home/email/jhsuccess');

                }else{

                }

               
            }

    /*
        前台登陆显示
    */
        public function homelogin()
        {
            return view('/home/login/login');
        }

    /*
        前台登陆
    */
        public function homedologin(Request $request)
        {
            
            $email = $request->username;
            $password = $request->passwd;
            
            // dd($password);
            $res = DB::table('dota_user')->where('email',$email)->first();

            
            if($res){
                $pwd = $res->password;
                if (Hash::check($password,$pwd) ){
                    
                 session(['loginemail'=>$email]);

                    return redirect('/home/index');

                  

                }else {echo "密码不正确";}

            }else{

                echo "没有此用户";
            }

           

        }

    /*
         前台退出登录
    */
        public function getLoginout()
        {
             echo "222222";die;
            // session()->forget('loginemail');
            // return redirect('/home/index');
        }




}
